package com.sui.bigdata.rtcadmin.model.vo;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.sui.bigdata.rtcadmin.enums.ResultCode;

/**
 * @Author: TingWuHuang
 * @Date: 2020/5/6 15:30
 * @description
 */
@SuppressWarnings("unchecked")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CommonResponse<T> {

    private String requestId;
    private Integer code;
    private String msg;
    private T data;

    public CommonResponse() {
    }

    public CommonResponse(ResultCode resultCode, T data) {
        this.code = resultCode.getCode();
        this.msg = resultCode.getMessage();
        this.data = data;
    }

    public CommonResponse(ResultCode resultCode, String requestId, T data) {
        this.requestId = requestId;
        this.code = resultCode.getCode();
        this.msg = resultCode.getMessage();
        this.data = data;
    }

    public CommonResponse(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public String getRequestId() {
        return requestId;
    }

    public void setRequestId(String requestId) {
        this.requestId = requestId;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static class Builder {

        public static CommonResponse buildSuccess(Object data) {
            return new CommonResponse(ResultCode.SUCCESS, data);
        }

        public static CommonResponse buildError(Object data) {
            return new CommonResponse(ResultCode.ERROR, data);
        }

        public static CommonResponse buildAuthFail(Object data) {
            return new CommonResponse(ResultCode.AUTH_FAIL, data);
        }

        public static CommonResponse build(ResultCode resultCode, Object data) {
            return new CommonResponse(resultCode, data);
        }

        public static CommonResponse buildWithId(ResultCode resultCode, String requestId, Object data) {
            return new CommonResponse(resultCode, requestId, data);
        }
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}